package hzst.android.form.info;

import hzst.android.entity.BaseUser;
import hzst.android.util.L;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
/**
 * 表单中控件绑定数据的基类
 * @author wt
 *
 */
public class BaseViewInfo implements Serializable{

	private static final long serialVersionUID = 1L;
	
	protected String viewType;
	protected String name = "";
	protected boolean mustFill;
	protected boolean visible = true;
	protected boolean readOnly = false;
	protected boolean special;
	protected int index;
	protected OnHasSpecialListener specialListener;
	protected BaseUser user;
	
	protected String submitInfo;//SubmitValue的json
	protected List<SubmitValue> submitValues;
	protected String specialValue;//SpecialValue的json
	protected List<SpecialValue> specialValues;
	protected List<BaseViewInfo> slaveTableInfos;//从表数据
	protected Map<String, String> parameters;//从表相关的任务数据

	protected String listInfo;

	/*
	 * 返回服务器字段值的类型
	 */
	public static final String TYPE_TEXT = "Text";
	public static final String TYPE_SV = "SelectedValue";
	public static final String TYPE_ST = "SelectedText";
	
	public String getViewType() {
		return viewType;
	}
	public void setViewType(String viewType) {
		this.viewType = viewType;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public boolean isSpecial() {
		return special;
	}

	public void setSpecial(boolean special) {
		this.special = special;
	}

	public boolean isMustFill() {
		return mustFill;
	}
	public void setMustFill(boolean mustFill) {
		this.mustFill = mustFill;
	}
	
	public String getSubmitInfo() {
		return submitInfo;
	}
	public void setSubmitInfo(String submitInfo) {
		this.submitInfo = submitInfo;
		parseSubmitInfo();
	}
	public String getListInfo() {
		return listInfo;
	}
	public void setListInfo(String listInfo) {
		this.listInfo = listInfo;
	}
	
	public boolean isReadOnly() {
		return readOnly;
	}
	public void setReadOnly(boolean readOnly) {
		this.readOnly = readOnly;
	}
	public OnHasSpecialListener getSpecialListener() {
		return specialListener;
	}
	public List<SubmitValue> getSubmitValues() {
		return submitValues;
	}
	public void setSubmitValues(List<SubmitValue> submitValues) {
		this.submitValues = submitValues;
	}
	public void setSpecialListener(OnHasSpecialListener specialListener) {
		this.specialListener = specialListener;
	}
	
	public Map<String, String> getParameters() {
		return parameters;
	}
	public void setParameters(Map<String, String> parameters) {
		this.parameters = parameters;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public boolean isVisible() {
		return visible;
	}
	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	public String getSpecialValue() {
		return specialValue;
	}
	public void setSpecialValue(String specialValue) {
		this.specialValue = specialValue;
		parseSpecialValue();
	}
	public List<SpecialValue> getSpecialValues() {
		return specialValues;
	}
	public void setSpecialValues(List<SpecialValue> specialValues) {
		this.specialValues = specialValues;
	}

	public BaseUser getUser() {
		return user;
	}

	public void setUser(BaseUser user) {
		this.user = user;
	}

	public List<BaseViewInfo> getSlaveTableInfos() {
		return slaveTableInfos;
	}
	public void setSlaveTableInfos(List<BaseViewInfo> slaveTableInfos) {
		this.slaveTableInfos = slaveTableInfos;
	}
	protected void parseSubmitInfo(){
		submitValues = new ArrayList<BaseViewInfo.SubmitValue>();
		
		try {
			JSONArray array = new JSONArray(submitInfo);
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj = array.getJSONObject(i);
				SubmitValue value = new SubmitValue();
				value.setFieldName(obj.getString("field"));
				value.setValue(obj.getString("value"));
				value.setValueType(obj.getString("type"));
				submitValues.add(value);
			}
		} catch (JSONException e) {
			L.showLogInfo(L.TAG_EXCEPTION, e.toString());
		}
	}
	
	private void parseSpecialValue(){
		specialValues = new ArrayList<BaseViewInfo.SpecialValue>();
		try {
			JSONArray array = new JSONArray(specialValue);
			for (int i = 0; i < array.length(); i++) {
				JSONObject obj = array.getJSONObject(i);
				SpecialValue value = new SpecialValue();
				value.setFieldName(obj.getString("field"));
				value.setValue(obj.getString("value"));
				specialValues.add(value);
			}
		} catch (JSONException e) {
//			L.showLogInfo(L.TAG_EXCEPTION, e.toString());
		}
	}
	
	/**
	 * 任务相关的数据(对于整个任务)
	 *
	 */
//	public class TransferParameter implements Serializable{
//		private static final long serialVersionUID = 1L;
//		private String fieldName;
//		private String value;
//
//		public String getFieldName() {
//			return fieldName;
//		}
//		public void setFieldName(String fieldName) {
//			this.fieldName = fieldName;
//		}
//		public String getValue() {
//			return value;
//		}
//		public void setValue(String value) {
//			this.value = value;
//		}
//
//	}
	/**
	 * 控件和服务器进行交互的主要数据，每个实例对应数据库中的一个字段。
	 *
	 */
	public class SubmitValue implements Serializable{
		private static final long serialVersionUID = 1L;
		
		private String valueType;
		private String fieldName;
		private String value = "";
		
		public String getValueType() {
			return valueType;
		}
		public void setValueType(String valueType) {
			this.valueType = valueType;
		}
		public String getFieldName() {
			return fieldName;
		}
		public void setFieldName(String fieldName) {
			this.fieldName = fieldName;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
		
	}
	/**
	 * 特殊事件的数据{@link #specialValues}
	 *
	 */
	public class SpecialValue implements Serializable{
		private static final long serialVersionUID = 1L;
		
		private String fieldName;
		private String value = "";
		
		public String getFieldName() {
			return fieldName;
		}
		public void setFieldName(String fieldName) {
			this.fieldName = fieldName;
		}
		public String getValue() {
			return value;
		}
		public void setValue(String value) {
			this.value = value;
		}
	}
	/**
	 * 特殊事件回调接口
	 *
	 */
	public interface OnHasSpecialListener{
		void doSpecial(Bundle bundle);
	}

}
